import { RecommendStatus } from '@/models/recommend'
import { PageParams, Status } from '@/models/sys'
import * as RecommendAPI from '@/service/index/recommend'
import * as TaskAPI from '@/service/index/task'
import * as UserInfoAPI from '@/service/index/user-info'
import { awaitTo } from '@/utils'

/**
 * 聊天
 */
interface IState {}

const useChat = () => {
  const state = reactive<IState>({})

  /** 推荐回复列表 */
  async function getRecommendReplyList(params: PageParams & { status: RecommendStatus }) {
    const { result } = await awaitTo(RecommendAPI.getRecommendReplyList(params))

    return result?.data?.data || []
  }

  /** 发送消息 */
  async function sendReply(params: { id: number; reply_content: string }, callback?: () => void) {
    const { result } = await awaitTo(RecommendAPI.sendRecommendReply(params))
    if (result) {
      callback && callback()
    }
  }

  /** 编辑信息 */
  async function editReply(params: { id: number; reply_content: string }, callback?: () => void) {
    const { result } = await awaitTo(RecommendAPI.updateRecommendReply(params))
    if (result) {
      callback && callback()
    }
  }

  /** 点踩 */
  async function dislikeReply(params: { id: number; is_bad: Status }, callback?: () => void) {
    const { result } = await awaitTo(RecommendAPI.dislikeRecommendReply(params))
    if (result) {
      callback && callback()
    }
  }

  /** 完成代办 */
  async function taskDone(params: { id: number; status: Status }, callback?: () => void) {
    const { result } = await awaitTo(TaskAPI.taskDone(params))
    if (result) {
      callback && callback()
    }
  }

  const todoTotal = ref(0)
  /** 任务列表 */
  async function getTaskList(params: PageParams) {
    const { result } = await awaitTo(TaskAPI.getTaskList(params))

    todoTotal.value = result?.data.meta.todo_count || 0
    return result?.data.data || []
  }

  /** AI提取信息确认 */
  async function getUserInfoList(params: PageParams) {
    const { result } = await awaitTo(UserInfoAPI.getUserInfoList(params))

    return result.data.data || []
  }

  /** 丢弃 */
  async function discardUserInfo(params: { id: number }, callback?: () => void) {
    const { result } = await awaitTo(UserInfoAPI.discardUserInfo(params))
    if (result) {
      callback && callback()
    }
  }

  /** 确定消息 */
  async function confirmUserInfo(
    params: { id: number; student_id: number },
    callback?: () => void,
  ) {
    const { result } = await awaitTo(UserInfoAPI.confirmUserInfo(params))
    if (result) {
      callback && callback()
    }
  }

  /** 编辑用户洞察 */
  async function editUserInsight(params: { id: number; content: string }, callback?: () => void) {
    const { result } = await awaitTo(UserInfoAPI.editUserInfo(params))
    if (result) {
      callback && callback()
    }
  }

  /** 删除用户洞察 */
  async function deleteUserInfo(params: { id: number }, callback?: () => void) {
    const { result } = await awaitTo(UserInfoAPI.deleteUserInfo(params))
    if (result) {
      callback && callback()
    }
  }

  /** 编辑用户画像 */
  async function editUserProfile(params: { id: number; value: string }, callback?: () => void) {
    const { result } = await awaitTo(UserInfoAPI.editUserPortrait(params))
    if (result) {
      callback && callback()
    }
  }

  return {
    ...toRefs(state),
    getRecommendReplyList,
    sendReply,
    dislikeReply,
    editReply,
    taskDone,
    getTaskList,
    todoTotal,
    getUserInfoList,
    discardUserInfo,
    confirmUserInfo,
    editUserInsight,
    deleteUserInfo,
    editUserProfile,
  }
}

export default useChat
